package com.hunttown.common.dao.base;

import com.hunttown.common.dao.sharding.ShardingRouters;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.util.Assert;

public class BaseShardingService extends BaseService {
    private static ShardingRouters shardingRouters;

    public BaseShardingService() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        super.setApplicationContext(applicationContext);
        if (shardingRouters == null) {
            if (applicationContext.containsBeanDefinition("shardingRouters")) {
                shardingRouters = (ShardingRouters)applicationContext.getBean("shardingRouters", ShardingRouters.class);
            }

            Assert.notNull(shardingRouters, "must define an ShardingRouters type bean, name : shardingRouters");
        }

    }

    public <T> T executeInShardingTrans(TransactionCallback<T> callback, Object... shardingObjs) {
        return transactionManagers.executeInShardingTrans(shardingRouters, callback, shardingObjs);
    }
}